﻿using System.Windows;
using System.Windows.Threading;

namespace System
{
    public class UiDispatcherHelper
    {
        /// <summary>
        /// 使用UI队列执行
        /// </summary>
        /// <param name="act"></param>
        public static void Invoke(Action act)
        {
            if (Dispatcher.CurrentDispatcher == Application.Current.Dispatcher)
                act();
            else
                Application.Current.Dispatcher.Invoke(act);
        }

        /// <summary>
        /// 使用UI队列执行
        /// </summary>
        /// <param name="act"></param>
        public static void Invoke(Action<object> act, object args)
        {
            if (Dispatcher.CurrentDispatcher == Application.Current.Dispatcher)
                act(args);
            else
                Application.Current.Dispatcher.Invoke(act, args);
        }

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <param name="action"></param>
        public static void BeginInvoke(Action act)
        {
            if (Dispatcher.CurrentDispatcher == Application.Current.Dispatcher)
                act.BeginInvoke(null, null);
            else
                Application.Current.Dispatcher.BeginInvoke(act);
        }
    }

}
